package com.py.credit.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.py.credit.common.ServiceException;
import com.py.credit.dao.ColumnRuleDao;
import com.py.credit.dao.RuleBindingDao;
import com.py.credit.dao.RuleCheckingDao;
import com.py.credit.entity.common.InspectRule;
import com.py.credit.service.ColumnRuleService;
import com.py.credit.utils.SQLBuilder;
import com.py.framework.core.common.Page;

@Service
public class ColumnRuleServiceImpl implements ColumnRuleService {

	@Autowired
	ColumnRuleDao columnRuleDao;

	@Autowired
	private RuleBindingDao ruleBindingDao;

	@Autowired
	private RuleCheckingDao ruleCheckingDao;

	/**
	 * 添加字段规则
	 */
	@Override
	public void add(String ruleName, String ruleDesc, String rulePackage, String ruleMethod, Long managerId,
			String time,String ruleMsg) {
		columnRuleDao.add(ruleName, ruleDesc, rulePackage, ruleMethod, managerId, time,ruleMsg);
	}

	/**
	 * 批量删除字段规则
	 */
	@Override
	public void del(String ids) {
		int total = ruleBindingDao.selectColRelationTotal(Arrays.asList(ids.split(",")));
		if (total > 0) {
			throw new ServiceException("该规则和字段存在绑定关系!");// 抛出异常
		}
		columnRuleDao.delete(Arrays.asList(ids.split(",")));
	}

	/**
	 * 获取单个字段数据
	 */
	@Override
	public InspectRule select(int id) {
		return columnRuleDao.select(id);
	}

	/**
	 * 修改字段规则
	 */
	@Override
	public void modify(int id, String ruleName, String ruleDesc, String rulePackage, String ruleMethod, Long managerId,
			String time,String ruleMsg) {
		columnRuleDao.modify(id, ruleName, ruleDesc, rulePackage, ruleMethod, managerId, time,ruleMsg);
	}

	/**
	 * 新增是查询同名
	 */
	@Override
	public int selectSameName(String ruleName) {
		return columnRuleDao.selectSameName(ruleName);
	}

	/**
	 * 规则校验
	 */
	@Override
	public void checkRule(int id, String data, String extend) {
		Class<?> c = null;
		Method m = null;
		boolean validate = false;
		InspectRule inspectRule = ruleCheckingDao.selectRuleForId(id);
		try {
			c = Class.forName(inspectRule.getRulePackage());
			m = c.getMethod(inspectRule.getRuleMethod(), String.class, String.class);

			validate = (boolean) m.invoke(c.newInstance(), data, extend);
			if (!validate) {
				throw new ServiceException("数据未通过校验!");
			}
		} catch (ClassNotFoundException e) {
			throw new ServiceException("未找到该规则!");
		} catch (NoSuchMethodException e) {
			throw new ServiceException("数据未通过校验");
		} catch (SecurityException e) {
			throw new ServiceException("数据未通过校验");
		} catch (IllegalAccessException e) {
			throw new ServiceException("数据未通过校验");
		} catch (IllegalArgumentException e) {
			throw new ServiceException("数据未通过校验");
		} catch (InvocationTargetException e) {
			throw new ServiceException("数据未通过校验");
		} catch (InstantiationException e) {
			throw new ServiceException("数据未通过校验");
		}

	}

	@Override
	public List<Map<String, Object>> getByCondition(String condition, List<String> columns) {
		List<String> conditions = SQLBuilder.createConditionSql(condition);

		return columnRuleDao.selectByCondition(columns, conditions);
	}

	/**
	 * 
	 * @Function 分页查询字段规则数据
	 * @FileName ColumnRuleServiceImpl.java
	 * @MethodName getByConditionForPage
	 * @param columns
	 * @param additionalColumns
	 * @param conditionStr
	 * @param page
	 * @param filedCode
	 * @return
	 * @Date 2018年7月26日 下午5:01:12
	 * @Author Min DongXv
	 *
	 */
	@Override
	public List<Map<String, Object>> getByConditionForPage(List<String> columns, String[] additionalColumns,
			String conditionStr, Page page, String fieldId) {
		List<String> conditions = SQLBuilder.createConditionSql(conditionStr);
		Integer version = ruleBindingDao.selectCurrentContentVersion();
		List<Map<String, Object>> batchLogs = null;
		if (version != null && version > 0) {
			batchLogs = columnRuleDao.selectByConditionForPage(columns, additionalColumns, conditions, page, version,
					fieldId);
		} else {
			batchLogs = columnRuleDao.selectNoVersionByConditionForPage(columns, additionalColumns, conditions, page);
		}
		return batchLogs;
	}

	@Override
	public List<InspectRule> getColumnRules(String ruleName) {

		return columnRuleDao.selectColumnRules(ruleName);

	}

	@Override
	public List<InspectRule> getBindRuleColumns(Integer version, String ruleId, String resourceName, Page page) {
		return columnRuleDao.selectBindRuleColumnsForPage(version, ruleId, resourceName, page);

	}
}
